﻿
; (function (factory) {
    if (typeof define === "function" && define.amd) {
        // AMD模式
        define(["jquery", "lyear-loading", "jquery-confirm", "bootstrap-notify"], factory);
    } else {
        // 全局模式
        factory(jQuery);
    }
}(function ($) {
    $.msgSuccess = function (message) {
        $.notify({
            icon: "mdi mdi-emoticon",
            title: "",
            message: message
        }, {
            allow_dismiss: false,
            type: "success",
            placement: {
                align: "right"
            },
            delay: 500,
            timer: 1000,
            template: '<div data-notify="container" class="col-xs-11 col-sm-3 alert alert-{0}" role="alert">' +
                '<button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>' +
                '<span data-notify="icon"></span> ' +
                '<span data-notify="title">{1}</span> ' +
                '<span data-notify="message">{2}</span>' +
                '<div class="progress" data-notify="progressbar">' +
                '<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>' +
                '</div>' +
                '<a href="{3}" target="{4}" data-notify="url"></a>' +
                '</div>'
        });
    }
    /**
    * 封装项目中用到的ajax请求
    * @param {any} url ajax请求路径
    * @param {any} data 参数
    * @param {any} successFn 默认请求成功后会弹出提示操作成功，若传入此参数则代替原有方法
    * @param {any} isLoading 是否显示loading
    * @param {any} isOpacity loading背景是否透明
    * @param {any} errorFn 请求失败后会弹出提示：操作失败，然后执行此函数
    * @param {any} type 请求类型
    * @param {any} async 是否异步
    * @param {any} dataType 返回数据类型
    * @param {any} success $.ajax的success
    * @param {any} error $.ajax的error
    */
    $.fn.ajaxRequest = function (url, data, successFn, isLoading = true, isOpacity = false, errorFn = null, type = "post", async = true, dataType = "json", success = null, error = null) {
        //loading
        var l;
        if (isLoading) {
            var opacity = 0;
            if (isOpacity) {
                opacity = 0.2;
            }
            l = $(this).lyearloading({
                opacity: opacity,
                spinnerSize: 'nm'
            });
        }

        $.ajax({
            url: url,
            type: type,
            async: async,
            data: data,
            dataType: dataType,
            success: function (data, textStatus) {

                if (success != null) {
                    success(data, textStatus)
                } else {
                    switch (data.code) {
                        case "200":
                            if (successFn) {
                                successFn(data, textStatus)
                            } else {
                                $.msgSuccess(data.message)

                            }
                            break;
                        case "401":
                            $.confirm({
                                title: '用户未登录',
                                icon: 'mdi mdi-alert',
                                type: 'orange',
                                escapeKey: true,
                                content: '点击确认按钮重新登录',
                                backgroundDismiss: true,
                                buttons: {
                                    okay: {
                                        text: '确认',
                                        keys: ['enter'],
                                        action: function () {
                                            window.location.reload();
                                        }
                                    },

                                },
                            });
                            break;
                        default:
                            $.alert({
                                title: data.message,
                                icon: 'mdi mdi-alert',
                                type: 'orange',
                                content: data.data,
                            });
                            if (errorFn != null) {
                                errorFn();
                            }
                            break;
                    }
                }
                if (isLoading) {
                    l.destroy();
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                if (error != null) {
                    error(XMLHttpRequest, textStatus, errorThrown);
                } else {
                    $.confirm({
                        title: '错误提示',
                        icon: 'mdi mdi-alert',
                        type: 'orange',
                        escapeKey: true,
                        content: '网络请求错误，请重试。',
                        backgroundDismiss: true,
                        buttons: {
                            okay: {
                                text: '确认',
                                keys: ['enter'],
                                action: function () {
                                    window.location.reload();
                                }
                            },
                            cancel: {
                                text: '取消',
                                keys: ['ctrl', 'shift'],
                                action: function () {

                                }
                            }
                        },
                    });
                }
                if (errorFn != null) {
                    errorFn();
                }
                if (isLoading) {
                    l.destroy();
                }
            }
        })
        return this;
    }
}));